<template>
<div>
    <validator name='v'>
        <form novalidate class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-2 control-lable">标题：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" v-model="notice.f_title" v-validate:f_title='{ required: true }' />
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-lable">内容：</label>
                <div class="col-sm-8">
                    <textarea class="form-control" type="text" v-model="notice.f_content" v-validate:f_content='{ required: true }'></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-lable">时间：</label>
                <div class="col-sm-8">
                    <input readonly='readonly' class="form-control" type="text" v-model="notice.f_time"/>
                </div>
            </div>
            <!-- <div class="form-group">
              <label class="col-sm-2 control-lable">分公司：</label>
              <div class="col-sm-8">
                <v-select :value.sync="f_division" :options='$parent.$refs.query.f_divisions' placeholder='请选择子公司'
                  close-on-select clear-button></v-select>
              </div>
            </div> -->
            <div class="form-group">
              <label class="col-sm-2 control-lable">分公司：</label>
               <v-select id="f_division"
              v-model="model.f_division"
              placeholder='请选择子公司'
              condition="ui.f_division ='{}'"
              :value.sync="model.f_division"
              :options='f_division'
              close-on-select clear-button>
              </v-select>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-lable"></label>
              <button type="button" class="btn btn-success col-sm-8" @click="save">

                 <span class="glyphicon glyphicon-floppy-disk"></span> 保存
              </button>
              <button type="button" class="btn btn-link col-sm-4" @click="anew">
                 <span class="glyphicon glyphicon-plus"></span> 新建
              </button>
              <button type="button" class="btn btn-success col-sm-4" @click="delete">
                 <span class="glyphicon glyphicon-trash"></span> 删除
              </button>
            </div>
        </form>
    </validator>
</div>

</template>

<script>
import * as Util from '../Util'
import co from 'co'
import Vue from 'vue'
import AppData from '../../stores/AppData'
let syncSave = function * (self) {
  yield self.$post('rs/entity/t_notice', {data: self.notice})
  self.$parent.$refs.query.$refs.cp.$refs.criteria.search()
}

let syncDelete = function * (self) {
  yield self.$remove('rs/entity/t_notice/', {id: '\'' + self.notice.id + '\''})
  self.$parent.$refs.query.$refs.cp.$refs.criteria.search()
}

export default {
  title: '公告表单',
  props: ['notice'],
  computed: {
    f_division: {
      get () {
        if(!this.notice || !this.notice.f_subcompany)
          return Vue.f_subcompany
        else
          return [this.notice.f_subcompany]
      },
      set (val) {
        this.notice.f_subcompany = val[0]
      }
    }
  },
  computed: {
    f_division () {
      return [{label: '全部', value: ''}, ...AppData.getParam('分公司')]
      // return [{label: '全部', value: ''}]
    },
  },
  methods: {
    save () {
      if(!this.notice)
      {
        alert('请选择一个通知。')
        return
      }
      if (!this.notice.id) {
        this.$set('notice.f_time', Util.toStandardTimeString())
      }
      let gen = syncSave(this)
      return co(gen)
    },
    anew () {
      this.notice = {}
      this.$set('notice.f_time', Util.toStandardTimeString())
    },
    delete () {
      if(!this.notice)
      {
        alert('请选择一个通知。')
        return
      }
      let gen = syncDelete(this)
      return co(gen)
    }
  }
}

</script>
